﻿using System;
using System.Collections;
using RazakiBLL;
using RazakiBLL.Managers;

namespace RazakiSite.UserControls.Forum
{
    public partial class UCMessagesList : BaseUserControl
    {
        public string Permission { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Permission != EMessagePermission.@private.ToString() || Page.User.Identity.IsAuthenticated)
            {
                GrdVMessage.Visible = true;
                LgnMessage.Visible = false;

                ConfigControls();
            }
            else
            {
                GrdVMessage.Visible = false;
                LgnMessage.Visible = true;

                LgnMessage.Url = "/PagesForum/MessagesList.aspx";
                LgnMessage.Message = "برای مشاهده لیست";
            }
        }

        private void ConfigControls()
        {
            UowRazaki uowRazaki = new UowRazaki();
            IList result = null;

            if (Permission == EMessagePermission.@private.ToString())
            {
                if (UtilitiesUI.IsAdmin())
                    result = uowRazaki.MessageForumRepository.GetAllPrivate(uowRazaki.MessagePermissionRepository);
                else
                    result = uowRazaki.MessageForumRepository.GetUserAllPrivate(uowRazaki.MessagePermissionRepository, UtilitiesUI.GetCurrentUserId());
            }
            else
            {
                result = uowRazaki.MessageForumRepository.GetAllPublic(uowRazaki.MessagePermissionRepository);
            }

            GrdVMessage.DataSource = result;
            GrdVMessage.DataBind();
        }
    }
}